<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .big {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 400px;
            height: 300px;
            background-color: pink;
        }

        .small {
            width: 400px;
            height: 70px;
            background-color: gray;
            cursor: move;
        }
    </style>
</head>

<body>
    <div class="big">
        <div class="small"></div>
    </div>
    <script>
        var big = document.querySelector('.big')
        var small = document.querySelector('.small')
        small.addEventListener('mousedown', function (e) {
            //鼠标在盒子内部的坐标
            var x = e.pageX - big.offsetLeft
            var y = e.pageY - big.offsetTop
            document.addEventListener('mousemove', move)
            function move(e) {
                //用鼠标相对文档的坐标 - 鼠标盒子内部的坐标 = 盒子应该移动的 左/ 上 边距
                big.style.left = e.pageX - x + 'px'
                big.style.top = e.pageY - y + 'px'
            }
            document.addEventListener('mouseup', function () {
                document.removeEventListener('mousemove', move)
            })
        })

    </script>
</body>

</html>